'use strict'
const path = require('path');
const webpack = require('webpack');
const { version, name } = require('./package')
function resolve(dir) {
  return path.join(__dirname, dir)
}
const Timestamp = new Date().getTime();
const port = 8091; // dev port
const publicPath = process.env.NODE_ENV==='development'?`/`:`${process.env.VUE_APP_WEB_PATH}`
let outputDir = 'kcbx';
name.split('-').forEach((item, index) => {
  outputDir += index>=1?item.replace(item[0], item[0].toUpperCase()):item
})

module.exports = {
  publicPath,
  outputDir,
  assetsDir: './static',
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    historyApiFallback: true,
    disableHostCheck: true,
    port,
    overlay: {
      warnings: false,
      errors: true
    },
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
    proxy: {
      '/api': {
        target: "http://test.hitcxedu.com:9334",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/api": ''
        }
      }
    }
  },
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    output: {
      library: `${name}-[name]`,
      libraryTarget: 'umd',
      jsonpFunction: `webpackJsonp_${name}`,
      filename: `./static/js/[name].${Timestamp}.js`,
      chunkFilename: `./static/js/[name].${Timestamp}.js`
    },
    //关闭 webpack 的性能提示
    performance: {
      hints: false
    },
  },
  chainWebpack(config) {
    const zipPath = `./${outputDir}-${version}-${process.env.ENV}.zip`;

    // 添加 svg sprite loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.plugin('provide').use(webpack.ProvidePlugin, [{
      '_': 'underscore',
    }])

    config.plugin('html').tap(args => {
      args[0].title = process.env.VUE_APP_NAME_CN
      return args;
    })

    if (process.env.NODE_ENV !== 'development') { // production
      config.plugin('FileManagerPlugin').use('filemanager-webpack-plugin', [{
        onEnd: {
          delete: [
            zipPath
          ],
          archive: [
            { source: `./${outputDir}`, destination: zipPath },
          ]
        }
      }])
        .end()

      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [{
          inline: /runtime\..*\.js$/
        }])
        .end()
    } else { // develop
      config.devtool('cheap-source-map')
    }
  },

  css: {
    // CSS source maps
    sourceMap: false,
    // CSS source maps
    sourceMap: false
  }
}
